﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using TouchDriver;

namespace GoomeiPicasaViewer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool mouseState = false;
        private Point mousePoint = new Point();
        private string[] fileList;
        private int fileNum;
        private Point orgintSize = new Point();
        private int angle = 0;

        private TouchDriver.TouchDriver driver = new TouchDriver.TouchDriver();
        public MainWindow()
        {
            this.InitializeComponent();
            this.Height = SystemParameters.WorkArea.Height;
            this.Width = SystemParameters.WorkArea.Width;
            LayoutRoot.Height = SystemParameters.WorkArea.Height;
            LayoutRoot.Width = SystemParameters.WorkArea.Width;
            string command = Environment.CommandLine;//获取进程命令行参数
            string[] para = command.Split('\"');
            string pathC = "";
            if (para.Length > 3)
            {
                pathC = para[3];//获取打开的文件的路径
            }
            else
        //        pathC = @"D:\Code\GoogleCode\WhiteBoard\GoomeiPicasaViewer\image\1.png";

       //     BitmapImage image = new BitmapImage(new Uri(pathC, UriKind.RelativeOrAbsolute));
      //      Picture.Source = image;
      //      orgintSize.X = image.Height;
      //      orgintSize.Y = image.Width;
            //Thickness former = PictureViewBox.Margin;
            //former.Left = (LayoutRoot.Width - image.Width) / 2;
            //former.Top = (LayoutRoot.Height - image.Height) / 2;
            //PictureViewBox.Margin = former;
            //PictureViewBox.Width = image.Width;
            //PictureViewBox.Height = image.Height;
            //fileList = System.IO.Directory.GetFiles(pathC.Substring(0, pathC.LastIndexOf('\\')), "*.jpg");

            
            driver.AlignCompletedEvent += new TouchDriver.TouchDriver.AlignCompletedHandler(driver_AlignCompletedEvent);
            driver.Start();

            //this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void driver_AlignCompletedEvent(bool aligned)
        {
            if (aligned)
            {
                driver.MouseClickEvent += new TouchDriver.TouchDriver.MouseClickHandler(driver_MouseClickEvent);
                driver.DoubleMoveEvent += new TouchDriver.TouchDriver.DoubleMoveHandler(driver_DoubleMoveEvent);
                driver.SingleMoveEvent += new TouchDriver.TouchDriver.SingleMoveHandler(driver_SingleMoveEvent);
            }
            else
            {
                MessageBox.Show("Close?");
                this.Close();
            }
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //driver.Start();
        }

        void driver_SingleMoveEvent(TouchDriver.Data.Location begin, TouchDriver.Data.Location end)
        {
            Mouse.Move(end.x - begin.x, end.y - begin.y);
        }

        void driver_DoubleMoveEvent(TouchDriver.Data.Record begin, TouchDriver.Data.Record end)
        {
            int beginWidth = abs(begin.loc1.x - begin.loc2.x);
            int beginHeight = abs(begin.loc1.y - begin.loc2.y);

            int endWidth = abs(end.loc1.x - end.loc2.x);
            int endHeight = abs(end.loc1.y - end.loc2.y);

            if ((endWidth - beginWidth > 0) || endHeight - beginHeight > 0)
            {
                Large();
            }
            else
            {
                Small();
            }
        }

        void driver_MouseClickEvent(TouchDriver.Data.Location loc)
        {
            Mouse.LeftClick(loc.x, loc.y);
        }

        private void ViewMouseDown(object sender, MouseButtonEventArgs e)
        {
            //mouseState = true;
            //mousePoint = e.GetPosition(LayoutRoot);
        }

        private void ViewMouseUp(object sender, MouseButtonEventArgs e)
        {
            //mouseState = false;
            if (mouseState)
                mouseState = false;
            else
            {
                mouseState = true;
                mousePoint = e.GetPosition(LayoutRoot);
            }
        }



        private void ViewMouseMove(object sender, MouseEventArgs e)
        {
            if (mouseState == true)
            {
                double top = mousePoint.Y - e.GetPosition(LayoutRoot).Y;
                double left = mousePoint.X - e.GetPosition(LayoutRoot).X;
                Thickness former = PictureViewBox.Margin;
                former.Left -= left;
                former.Top -= top;
                PictureViewBox.Margin = former;
                mousePoint = e.GetPosition(LayoutRoot);
            }
        }

        private void ellipse_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            this.Close();
        }
        private void Large()
        {
            Thickness former = PictureViewBox.Margin;
            former.Left -= PictureViewBox.Width * 0.05;
            former.Top -= PictureViewBox.Height * 0.05;
            PictureViewBox.Margin = former;
            PictureViewBox.Height *= 1.1;
            PictureViewBox.Width *= 1.1;
        }
        private void Small()
        {
            Thickness former = PictureViewBox.Margin;
            PictureViewBox.Height /= 1.1;
            PictureViewBox.Width /= 1.1;
            former.Left += PictureViewBox.Width * 0.05;
            former.Top += PictureViewBox.Height * 0.05;
            PictureViewBox.Margin = former;
        }
        private void Origin()
        {
            Thickness former = PictureViewBox.Margin;
            PictureViewBox.Height = orgintSize.X;
            PictureViewBox.Width = orgintSize.Y;
            former.Left = (LayoutRoot.Width - orgintSize.Y) / 2;
            former.Top = (LayoutRoot.Height - orgintSize.X) / 2;
            PictureViewBox.Margin = former;
        }
        private void LayoutRoot_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0)
            {
                Large();
            }
            if (e.Delta < 0)
            {
                Small();
            }
        }

        private void Next_Click(object sender, RoutedEventArgs e)
        {
            BitmapImage image = new BitmapImage(new Uri(fileList[fileNum], UriKind.RelativeOrAbsolute));
            ++fileNum;
            if (fileNum == fileList.Length)
                fileNum = 0;
            Picture.Source = image;
            Thickness former = PictureViewBox.Margin;
            former.Left = (LayoutRoot.Width - image.Width) / 2;
            former.Top = (LayoutRoot.Height - image.Height) / 2;
            PictureViewBox.Margin = former;
            PictureViewBox.Width = image.Width;
            PictureViewBox.Height = image.Height;
        }

        private void former_Click(object sender, RoutedEventArgs e)
        {
            BitmapImage image = new BitmapImage(new Uri(fileList[fileNum], UriKind.RelativeOrAbsolute));
            --fileNum;
            if (fileNum == -1)
                fileNum = fileList.Length - 1;
            Picture.Source = image;
            Thickness former = PictureViewBox.Margin;
            former.Left = (LayoutRoot.Width - image.Width) / 2;
            former.Top = (LayoutRoot.Height - image.Height) / 2;
            PictureViewBox.Margin = former;
            PictureViewBox.Width = image.Width;
            PictureViewBox.Height = image.Height;
        }
        private void Rotate()
        {
            RotateTransform rotateTransform = new RotateTransform(90 * angle);
            rotateTransform.CenterX = Picture.Width / 2;
            rotateTransform.CenterY = Picture.Height / 2;
            Picture.RenderTransform = rotateTransform;
            //PictureViewBox.Width = image.Width;
            //PictureViewBox.Height = image.Height;
        }
        private void CounterClock_Click(object sender, RoutedEventArgs e)
        {
            --angle;
            Rotate();
        }

        private void Large_Grid_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            Large();
        }

        private void Small_Grid_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            Small();
        }

        private void Origin_Grid_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            Origin();
        }

        private void CounterClock_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            --angle;
            Rotate();
        }

        private void Counter_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            ++angle;
            Rotate();
        }

        private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //ListViewItem item = (ListViewItem)listImages.SelectedItem;
            //BitmapImage image = new BitmapImage(new Uri(item.Tag.ToString(), UriKind.RelativeOrAbsolute));
            //Picture.Source = image;
        }

        private int abs(int a)
        {
            return (a < 0) ? -a : a;
        }

        private void listImages_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ListViewItem item = (ListViewItem)listImages.SelectedItem;
            BitmapImage image = new BitmapImage(new Uri(item.Tag.ToString(), UriKind.RelativeOrAbsolute));
            Picture.Source = image;
        }
    }
}